Dropoutgrad

计算 Dropout 操作的梯度。

\[dx_i = dy_i \cdot \text{mask}_i \cdot \text{scale}\]

其中 \(dy_i\) 是来自后一层的上游梯度,\(\text{mask}_i\) 是前向传播时使用的同一个掩码,\(\text{scale}\) 是前向传播时使用的同一个缩放因子。

输入:
  • input - 上游梯度张量的数据地址 (dy)。

  • scale - 前向传播时使用的缩放因子。

  • length - 张量的总元素数量。

  • mask - 前向传播时使用的掩码张量的数据地址。

  • core_mask - 核掩码。

输出:
  • output - 输出的梯度张量的数据地址 (dx)。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持fp32

  • MT7004 支持fp16, fp32

共享存储版本:

void fp_dropoutgrad_s(float *input, float scale, int length, float *output, float mask, int core_mask)
void hp_dropoutgrad_s(half *input, half scale, int length, half *output, half mask, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <dropoutgrad.h>
 4int main(int argc, char* argv[]) {
 5    float *dy = (float *)0xA0000000;    // Upstream gradient (dy), DDR
 6    float *dx = (float *)0xB0000000;    // Output gradient (dx)
 7    float *mask = (float *)0xC0000000;  // Mask from forward pass
 8
 9    int length = 4096;
10    // 假设前向传播时 dropout 概率 p = 0.2
11    float scale = 1.0f / (1.0f - 0.2f); // scale = 1.25
12    int core_mask = 0xff;
13
14    fp_dropoutgrad_s(dy, scale, length, dx, mask, core_mask);
15    return 0;
16}

私有存储版本:

void fp_dropoutgrad_p(float *input, float scale, int length, float *output, float mask)
void hp_dropoutgrad_p(half *input, half scale, int length, half *output, half mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <dropoutgrad.h>
 4int main(int argc, char* argv[]) {
 5    float *dy = (float *)0x10000000;    // Upstream gradient (dy), L2
 6    float *dx = (float *)0x11000000;    // Output gradient (dx)
 7    float *mask = (float *)0x12000000;  // Mask from forward pass
 8
 9    int length = 1024;
10    // 假设前向传播时 dropout 概率 p = 0.5
11    float scale = 1.0f / (1.0f - 0.5f); // scale = 2.0
12
13    fp_dropoutgrad_p(dy, scale, length, dx, mask);
14    return 0;
15}